package cn.itsource.org.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.Constant;
import cn.itsource.basic.util.MailService;
import cn.itsource.basic.util.RedisService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;

@Service
//Propagation.SUPPORTS:支持别人事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop, ShopQuery> implements IShopService {
    //基础查询由继承得到

    @Autowired
    private RedisService redisService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private MailService mailService;

    @Override
    @Transactional
    public AjaxResult activation(String uuid) {
        //1.通过uuid获取存进去shop对象,如果没有,激活已经失效
        Shop shop = redisService.getKeyObjectValue(uuid, Shop.class);
        Optional.ofNullable(shop).orElseThrow(()->new BusinessException("店铺激活链接失效,请直接联系平台,打911"));

        //健壮性判断,如果已经是激活状态,告诉他不需要再激活  1.查一把  2.判断

        //2.如果有,拿到shop对象
        //3.把shop的状态字段改成3
        shop.setState(Constant.Shop.STATE_ACTIVE_SUCCESS);
        shopMapper.updateByPrimaryKeySelective(shop);

        //激活成功
        return AjaxResult.createSuccess("激活成功");
    }


    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @Override
    @Transactional
    public AjaxResult settlement(Shop shop) {
        //1.验证两次密码必须是一致的
        Employee admin = shop.getAdmin();

        //判断admin是否为null  如果为null抛出自定义的业务异常
        Optional.ofNullable(admin).orElseThrow(()->new BusinessException("系统异常,联系管理员"));

        //判断密码
        String password = admin.getPassword();
        String comfirmPassword = admin.getComfirmPassword();

        if (!StringUtils.equals(password,comfirmPassword)){
            throw new BusinessException("两次输入密码不一致");
        }

        //1.1验证账户不能重复
        Employee employee = employeeMapper.selectInfo(admin.getUsername());
        if (employee != null){
            throw new BusinessException("用户已存在,请换个名字");
        }


        //加密
  /*    String salt = IdUtil.fastSimpleUUID();
        System.out.println(salt);

        String md5Pwd = MD5.create().setSalt(salt.getBytes()).digestHex(password);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);
    */

        admin.init();

        //接下来就是店铺入驻的流程了
        //2.先新增员工,拿到员工自增长id
        employeeMapper.insertSelective(admin);

        //3.把员工的自增id设置到shop对象 ,添加shop对象  ,拿到shop的id
        shop.setAdminId(admin.getId());
        shopMapper.insertSelective(shop);

        //4.修改员工  ,把拿到shopId修改到店长员工上面
        admin.setShopId(shop.getId());
        employeeMapper.updateByPrimaryKeySelective(admin);

        String uuid = IdUtil.fastSimpleUUID();
        String url = "http://localhost:80/shop/activation/"+uuid;

        //保存redis
        redisService.setStringKeyAndValue(uuid,shop,2);

        //5.发送邮件
        mailService.sendMail("宠物乐园激活","请点击链接激活你的宠物乐园店铺,"+url,admin.getEmail());

        return AjaxResult.createSuccess();
    }

}
